<?php
class Model_Order_Order extends Model_System {
	public $_name = "order";
	protected $_sequence = true;
	public function getAll() {
		$result = $this->select();
		$result->order("id DESC");
		return $this->fetchAll($result)->toArray();	

	}
	public function getAllStartWith($char) {
		$result = $this->select();
		$result->where('email like ?',$char.'%');
		$result->order("id DESC");
		return $this->fetchAll($result)->toArray();	

	}
	public function getAllOrder($name,$order) {
		$result = $this->select();
		$result->order($name." ".$order);
		return $this->fetchAll($result)->toArray();
	
	}
	public function checkPassword($id,$password)
	{
	    $data = $this->select();
		$data -> where('id = ?',$id);
		$item = $this->fetchRow($data)->toArray();
		$oldpassword = $item['password'];
		$passwordtemp = md5($password.'-'.$item['hash']);
		if($passwordtemp==$oldpassword)
		    return true;
		else
		    return false;
	}
	public function getHashByEmail($email)
	{
		$result = $this->select();
		$result->where('email = ?',$email);
		$data = $this->fetchRow($result);
		//$result là biến kiểu object (toArray() là biến kiểu mảng)
		if($data!='')
			return $data->hash;
		else 
			return '';
	}
	public function getHashById($id)
	{
		$result = $this->select();
		$result->where('id = ?',$id);
		$data = $this->fetchRow($result);
		//$result là biến kiểu object (toArray() là biến kiểu mảng)
		if($data!='')
			return $data->hash;
		else
			return '';
	}
	public function getRecentUser($num){
		$result = $this->select();
		$result->order("date_cre DESC");
		$result->limit($num,0);
		return $this->fetchAll($result)->toArray();	
	}
	public function checkEmail($email){
		$result = $this->select();
		$result->where('email = ?',$email);
		$data = $this->fetchAll($result)->toArray();	
		if(count($data)>0)
			return true;
		else
			return false;
	}
	public function CheckValidPass($id, $oldpass) {
		$query = $this->select();
		$query->where('id = ?',$id);
		$result = $this->fetchRow($query)->toArray();
		if($result['password'].'-'.$result['hash']==md5($oldpass))
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	public function getItemId($id){
		$result = $this->select();
		$result->where('id = ?',$id);
		return $this->fetchRow($result)->toArray();
	}
	static function getNamebyId($id)
	{
		$excute = new Model_Webmaster_Webmaster();
		$query = $excute->select();
		$query->where('id = ?',$id);
		$result = $excute->fetchRow($query)->toArray();
		return $result['last_name'].' '.$result['first_name'];
	}
	public function GetAllUserSercure() {
		$sql = $this->select();
		$result = $sql->where('type != 1');
		return $this->fetchAll($result)->toArray();
	}
	public function insertItem($data)
	{
		try {
		    $this->insert($data);
		} 
		catch (Exception $e) {
		    return $e->getMessage();
		}
		return "success";
		
	}
	public function updateItem($data,$id){
		try {
		    $this->update($data, "id = $id");
		} 
		catch (Exception $e) {
		    return $e->getMessage();
		}
		return "success";
	}
}